package com.megvii.koala.tv.presenter;

import android.os.Handler;
import android.os.Looper;

import com.megvii.koala.base.KoalaPresenter;
import com.megvii.koala.guard.Box;
import com.megvii.koala.tv.box.BoxManuallyManager;
import com.megvii.koala.tv.data.SurveillanceCameraManager;
import com.megvii.koala.tv.data.TVBoxManager;
import com.megvii.koala.tv.data.TVGlobalData;
import com.megvii.koala.tv.dialog.BoxAddDialog;
import com.megvii.koala.tv.fragment.BoxSettingFragment;
import com.megvii.koala.tv.fragment.RecognizeCamerasSettingFragment;

/**
 * Created by linyue on 16/1/22.
 */
public class BoxSettingPresenter extends KoalaPresenter<BoxSettingFragment>
        implements BoxSettingFragment.BoxSettingListener, TVBoxManager.TVBoxManagerListener, BoxManuallyManager.BoxManuallyManagerListener, BoxAddDialog.BoxAddDialogListener {
    private TVBoxManager boxManager;

    private SurveillanceCameraManager cameraManager;

    private BoxManuallyManager boxManuallyManager;

    private BoxAddDialog boxAddDialog;

    private Handler handler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreated() {
        super.onCreated();
        boxManager = TVGlobalData.instance().boxManager;
        cameraManager = TVGlobalData.instance().cameraManager;
        boxManager.registerListener(this);
        viewHolder.setListener(this);
        boxManager.search();
        boxManuallyManager = new BoxManuallyManager();
        boxManuallyManager.init();
        boxManuallyManager.setListener(this);
        boxAddDialog = new BoxAddDialog();
        boxAddDialog.setListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewHolder.updateBoxes(boxManager.getAddresses());
        //
        String selectedAddress = boxManager.getSelectedAddress();
        String selectedToken = boxManager.getSelectedToken();
        if (selectedAddress != null && selectedToken != null)
            boxManager.addBox(selectedAddress, selectedToken);
    }

    @Override
    protected void onDestory() {
        super.onDestory();
        boxManager.unregisterListener(this);
        boxManuallyManager.release();
        boxAddDialog.setListener(null);
    }

    @Override
    public void onNewBoxSearched(Box box) {
        viewHolder.updateBoxes(boxManager.getAddresses());
    }

    @Override
    public void onSelectionChanged(Box box) {

    }

    @Override
    public void onClearSelection() {

    }

    @Override
    public void onBackPressed() {
        viewHolder.finish();
    }

    @Override
    public void onChooseItem(int position) {
        cameraManager.persistentClear();
        Box box = boxManager.getBox(position);
        boxManager.selectBox(box);
        viewHolder.startFragment(RecognizeCamerasSettingFragment.class, null);
    }

    @Override
    public void onClickAddBoxButton() {
        boxAddDialog.show(viewHolder.getContextX());
    }

    @Override
    public void onBoxValid(final String address, final String token) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                boxAddDialog.hide();
                viewHolder.hideProgressBar();
                boxManager.addBox(address, token);
            }
        });
    }

    @Override
    public void onBoxInvalid(final String address) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                boxAddDialog.hide();
                viewHolder.hideProgressBar();
                showToast("地址输入有误, 或主机不存在");
            }
        });
    }

    @Override
    public void onAddBoxAddress(String address) {
        boolean success = boxManuallyManager.setBoxAsync(address);
        if (success) {
            viewHolder.showProgressBar();
        } else {
            showToast("正在查找主机, 请稍后再试");
        }
    }

    @Override
    public void onCancelBoxAddDialog() {
        boxAddDialog.hide();
        viewHolder.hideProgressBar();
    }
}
